<template>
  <page-header-wrapper :title="false">
    <a-card title="活动详情">
      <a-descriptions bordered title="" size="small">
        <a-descriptions-item label="活动名称" :span="3"> {{ info?.name }} </a-descriptions-item>
        <a-descriptions-item label="轮次" :span="3"> {{ this.$route.query.activity }} </a-descriptions-item>
        <a-descriptions-item label="开始时间" :span="3"> {{ info?.beginDate }} </a-descriptions-item>
        <a-descriptions-item label="结束时间" :span="3"> {{ info?.endDate }} </a-descriptions-item>
      </a-descriptions>
    </a-card>
    <a-card title="参与列表" style="margin-top: 24px">
      <template #extra>
        <a-tooltip>
          <template #title> 数据导出</template>
          <a-button type="primary" icon="database" @click="handleExpert(queryParam)"></a-button>
        </a-tooltip>
      </template>
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="8">
              <a-form-item label="筛选">
                <a-input v-model="queryParam.keyword" placeholder="请输入筛选" />
              </a-form-item>
            </a-col>
            <a-col :md="(!advanced && 8) || 24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" @click="handleSearch(queryParam)" :loading="searchLoading">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <a-table
        ref="table"
        size="default"
        rowKey="key"
        :columns="columns"
        :loading="searchLoading"
        :data-source="loadData"
        :pagination="pagination"
        @change="handleTableChange"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="action" slot-scope="text, record">
          <template>
            <a-tag color="#428bca" @click="handleDetail(record)">进入</a-tag>
          </template>
        </span>
      </a-table>
    </a-card>
    <a-card title="参与途径统计" style="margin-top: 24px">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="8">
              <a-form-item label="筛选">
                <a-input v-model="queryParam1.keyword" placeholder="请输入筛选" />
              </a-form-item>
            </a-col>
            <a-col :md="(!advanced && 8) || 24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" @click="handleSearch1(queryParam1)" :loading="recordLoading">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <a-table
        ref="table"
        size="default"
        rowKey="key"
        :columns="recordcolumns"
        :loading="recordLoading"
        :data-source="recordloadData"
        :pagination="recordPagination"
        @change="handleTableChange1"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { getMemActivityUsingPOST, getMemActJoinListUsingPOST,getActPathTotalUsingPOST,exportMemActJoinListUsingPOST } from '@/api/api/huiyuanguanli.ts'
export default {
  name: 'MemberListRoundDetail',
  components: {},
  data() {
    return {
      info: {},
      advanced: false,
      priceMdll: null,
      editMdll: null,
      priceVisible: false,
      editVisible: false,
      priceLoading: false,
      editLoading: false,
      searchLoading: false,
      recordLoading: false,
      queryParam: {
        pageNo: 1,
        pageSize: 10,
        queryCondition: '',
        status: '',
      },
      pagination: {},
      columns: [
        {
          title: '会员名',
          dataIndex: 'name',
        },
        {
          title: '联系方式',
          dataIndex: 'phone',
        },
        {
          title: '会员卡号',
          dataIndex: 'cardNo',
        },
        {
          title: '参与时间',
          dataIndex: 'joinDate',
        },
        {
          title: '获得积分',
          dataIndex: 'getIntegral',
        },
        {
          title: '参与途径',
          dataIndex: 'path',
        },
      ],
      queryParam1: {
        pageNo: 1,
        pageSize: 10,
        queryCondition: '',
        status: '',
      },
      loadData: [],
      recordcolumns: [
        {
          title: '参与途径',
          dataIndex: 'path',
        },
        {
          title: '参与人次',
          dataIndex: 'joinNum',
        },
        
        
      ],
      recordloadData: [],
      recordPagination: {},
    }
  },
  mounted() {
    this.getDetail()
    this.handleSearch(this.queryParam)
    this.handleSearch1(this.queryParam)
  },
  methods: {
    handleExpert(param){
      exportMemActJoinListUsingPOST({ ...param, orderId: this.$route.params.id}).then((res) => {
          const blob = new Blob([res], {
            type: 'application/vnd.ms-excel;charset=utf-8',
          })
          const filename = '参与列表' + '.xlsx'
          // 将blob对象转为一个URL
          var blobURL = window.URL.createObjectURL(blob)
          // 创建一个a标签
          var tempLink = document.createElement('a')
          // 隐藏a标签
          tempLink.style.display = 'none'
          // 设置a标签的href属性为blob对象转化的URL
          tempLink.href = blobURL
          // 给a标签添加下载属性
          tempLink.setAttribute('download', filename)
          if (typeof tempLink.download === 'undefined') {
            tempLink.setAttribute('target', '_blank')
          }
          // 将a标签添加到body当中
          document.body.appendChild(tempLink)
          // 启动下载
          tempLink.click()
          // 下载完毕删除a标签
          document.body.removeChild(tempLink)
          window.URL.revokeObjectURL(blobURL)
          this.$message.success('导出成功~')
        })  
    },
    handleSearch1(param) {
      this.recordLoading = true
      getActPathTotalUsingPOST({ ...param, orderId: this.$route.params.id })
        .then((res) => {
          this.recordLoading = false
          this.loadData = res.data.list
          this.queryParam1.pageNo = this.recordPagination.current
          this.recordPagination = { ...this.recordPagination, total: Number(res.data.total) }
        })
        .catch((error) => {
          this.recordLoading = false
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    handleTableChange1(pagination, filters, sorter) {
      const pager = { ...this.recordPagination }
      pager.current = recordPagination.current
      pager.pageSize = recordPagination.pageSize
      this.recordPagination = pager
      this.handleSearch1({
        sortField: sorter.field,
        sortOrder: sorter.order,
        ...filters,
        pageNo: pagination.current,
        pageSize: pagination.pageSize,
        queryCondition: this.queryParam1.queryCondition,
        status: this.queryParam1.status,
      })
    },
    handleDetail(record) {
      this.$router.push({ path: `/member/list/scoreactivity/roundDetail/${record.activityId}` })
    },
    getDetail() {
      getMemActivityUsingPOST({ id: this.$route.params.id })
        .then((res) => {
          this.info = res.data
        })
        .catch(() => {
          this.$message.error('获取详情失败,请联系管理员')
        })
    },
    handleSearch(param) {
      this.searchLoading = true
      getMemActJoinListUsingPOST({ ...param, orderId: this.$route.params.id })
        .then((res) => {
          this.searchLoading = false
          this.loadData = res.data.list
          this.queryParam.pageNo = this.pagination.current
          this.pagination = { ...this.pagination, total: Number(res.data.total) }
        })
        .catch((error) => {
          this.searchLoading = false
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    handleTableChange(pagination, filters, sorter) {
      const pager = { ...this.pagination }
      pager.current = pagination.current
      pager.pageSize = pagination.pageSize
      this.pagination = pager
      this.handleSearch({
        sortField: sorter.field,
        sortOrder: sorter.order,
        ...filters,
        pageNo: pagination.current,
        pageSize: pagination.pageSize,
        queryCondition: this.queryParam.queryCondition,
        status: this.queryParam.status,
        date: this.queryParam.date,
      })
    },
  },
}
</script>
